<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-title">NEWBE工作室订单管理系统</div>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            prefix-icon="el-icon-user"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            prefix-icon="el-icon-lock"
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        <el-form-item>
          <div class="remember-row">
            <el-checkbox v-model="loginForm.rememberMe">保持登录状态(7天)</el-checkbox>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button :loading="loading" type="primary" class="login-button" @click="handleLogin">
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
/* eslint-disable vue/multi-word-component-names */
/* eslint-disable no-undef */
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { useUserStore } from '@/stores/user';

defineOptions({
  name: 'LoginPage'
});

const router = useRouter();
const userStore = useUserStore();

const loginForm = reactive({
  username: '',
  password: '',
  rememberMe: true
});

const loginRules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
};

const loading = ref(false);
const loginFormRef = ref(null);

const handleLogin = async () => {
  if (!loginFormRef.value) return;

  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true;
      try {
        console.log('准备发送登录请求数据:', {
          username: loginForm.username,
          password: loginForm.password,
          rememberMe: loginForm.rememberMe
        });
        
        await userStore.userLogin(loginForm.username, loginForm.password, loginForm.rememberMe);
        ElMessage.success('登录成功');
        // 重定向到订单明细记录页
        router.push('/');
      } catch (error) {
        console.error('登录失败:', error);
        if (error.response) {
          console.error('错误状态码:', error.response.status);
          console.error('错误数据:', error.response.data);
        }
        
        // 只在axios拦截器未处理的情况下显示错误消息
        // 401错误已在request.js的拦截器中处理，这里不需要再次处理
        if (!error.response || error.response.status !== 401) {
          // 显示具体的错误信息
          let errorMsg = '登录失败';
          
          // 尝试从响应中获取更具体的错误信息
          if (error.response && error.response.data && error.response.data.message) {
            errorMsg = error.response.data.message;
          } else if (error.message) {
            errorMsg = error.message;
          }
          
          ElMessage({
            message: errorMsg,
            type: 'error',
            duration: 5000
          });
        }
      } finally {
        loading.value = false;
      }
    }
  });
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
}

.login-box {
  width: 350px;
  padding: 30px;
  background-color: white;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  font-size: 22px;
  font-weight: bold;
  margin-bottom: 30px;
  color: #409EFF;
}

.login-form {
  margin-top: 20px;
}

.login-button {
  width: 100%;
}

.remember-row {
  margin-bottom: 20px;
}
</style> 